package com.gardenguell.salad.ui.fragment;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.gardenguell.salad.R;
import com.gardenguell.salad.app.HandlerCenter;
import com.gardenguell.salad.data.client.SaladClient;
import com.gardenguell.salad.data.model.BaseModel;
import com.gardenguell.salad.data.model.Vegetable;
import com.gardenguell.salad.data.volley.OnSuccessResponse;
import com.gardenguell.salad.ui.activity.BoxActivity;
import com.gardenguell.salad.ui.activity.VegetableActivity;
import com.gardenguell.salad.ui.activity.base.ToolbarActivity;
import com.gardenguell.salad.ui.adapter.BoxAdapter;
import com.gardenguell.salad.ui.adapter.ClickableRecyclerViewAdapter;
import com.gardenguell.salad.ui.adapter.DividerLineDecoration;
import com.gardenguell.salad.ui.adapter.VegetableAdapter;
import com.gardenguell.salad.util.DisplayUtil;
import com.gardenguell.salad.util.ResUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by absurd on 16/2/12.
 */
public class VegetableFragment extends BaseFragment{


    private RecyclerView mVegetableList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_vegetable, container, false);



        List<Vegetable> data = Vegetable.getVegetableList();
        VegetableAdapter vegetableAdapter = null;
        if(getBaseActivity() instanceof  BoxActivity) {
            ((BoxActivity) getBaseActivity()).showToolbar();
            ((BoxActivity) getBaseActivity()).setToolbarTitle(ResUtil.getString(R.string.vegetable_library));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
                getBaseActivity().getWindow().setStatusBarColor(ResUtil.getColor(R.color.dark_color_primary));
            if (getBaseActivity() instanceof ToolbarActivity) {
                ((ToolbarActivity) getBaseActivity()).setToolbarColor(ResUtil.getColor(R.color.dark_color_primary));
            }
            vegetableAdapter = new VegetableAdapter(data, false);
        }else{
            vegetableAdapter = new VegetableAdapter(data, true);
        }


        mVegetableList = (RecyclerView)rootView.findViewById(R.id.vegetable_list);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.getBaseActivity());
        mVegetableList.setLayoutManager(layoutManager);
        // set every item's height is fixed
        mVegetableList.setHasFixedSize(true);


        vegetableAdapter.setOnRecyclerViewItemClickListener(new ClickableRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, BaseModel baseModel) {
                SaladClient client = new SaladClient();
                switch(view.getId()){
                    case R.id.btn_add:
                        if(getBaseActivity() instanceof VegetableActivity){
                            String mac = ((VegetableActivity)getBaseActivity()).mac;
                            int unitId = ((VegetableActivity)getBaseActivity()).unitId;
                            client.addVegetable(mac, unitId, getUsername(),((Vegetable)baseModel).getId(), new OnSuccessResponse() {
                                @Override
                                public void onSuccessWithNoBody() {
                                    super.onSuccessWithNoBody();
                                    VegetableFragment.this.getBaseActivity().finish();
                                    HandlerCenter.getInstance().getHandler(HandlerCenter.Flag.UNIT_FRAGMENT)
                                            .sendEmptyMessage(HandlerCenter.Message.REFRESH_UNIT_LIST);
                                }
                            }, VegetableFragment.this);
                        }else if(getBaseActivity() instanceof BoxActivity) {

                        }
                        break;
                    default:
                        break;
                }
            }
        });
        mVegetableList.setAdapter(vegetableAdapter);
        DividerLineDecoration dividerLine = new DividerLineDecoration();
        dividerLine.setSize(DisplayUtil.dip2px(this.getBaseActivity(),10));
        dividerLine.setColor(ResUtil.getColor(R.color.main_background));
        mVegetableList.addItemDecoration(dividerLine);
        return rootView;
    }


}
